<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body{
            background: skyblue;
        }
        .box{
            width: 300px;
            height: 300px;
            background: aquamarine;
            margin: 50px auto;
            border: 1px solid #ccc;
            border-radius: 1px;
        }
        .box label{
            display: inline-block;
            width: 70px;
            margin: 8px;
        }
        button{
            margin:10px 0 0 120px;
        }
    </style>
</head>
<body>
    <div class="box">
        <label for="username">用户名</label>
        <input type="text" id="username">
        <br/>
        <label for="nickname">昵称</label>
        <input type="text" id="nickname">
        <br/>
        <label for="mail">邮箱</label>
        <input type="text" id="mail">
        <br/>
        <label for="id">身份证</label>
        <input type="text" id="idnum">
        <br/>
        <label for="phonenumble">手机号码</label>
        <input type="text" id="phonenumble">
        <br/>
        <label for="birthday">生日</label>
        <input type="text" id="birthday">
        <br/>
        <label for="password">密码</label>
        <input type="text" id="password">
        <br/>
        <button id="btn">提交</button>
    </div>

    <script>
        //用户名不能为空
        //不能使用特殊字符（只能使用数字、字母、下划线、横杠）
        //必须以字母开头
        //长度6-20
        var username = document.getElementById('username');
        var user = username.value
        var userReg = /^[a-zA-Z][0-9a-zA-Z_-]{5,19}$/; 
        // var str = 'a520520-_';
        // console.log(userReg.test(str));
        var res = userReg.exec(user);
        
        btn.onclick = () =>{
            if(user == ''){
                alert('用户名不能为空');
                username.style.background = 'pink';
            }else if(res !== true){
                alert('请输入以字母开头，长度为6-20，由字母、数字、下划线、横杠组成的字符');
            }
        }

    </script>
</body>
</html>